import defineProperty from './defineProperty';
import {def} from './utils';
import {arrayMethods} from './array';
import observe from "./observe";
import Dep from "./Dep";

export default class Observer {

    constructor(value) {
        this.dep = new Dep();

        def(value, '__ob__', this, false);

        if (value instanceof Array) {
            Object.setPrototypeOf(value, arrayMethods);
            this.observeArray(value);
        } else {
            this.walk(value);
        }
    }

    walk(obj) {
        for (const key in obj) {
            defineProperty(obj, key);
        }
    }

    observeArray(arr) {
        arr.forEach(val => {
            if (val instanceof Object) {
                observe(val);
            }
        });
    }
}